BASEDIR = ../
RELEASE = ../release

include $(BASEDIR)/version

INCLUDE = -I$(BASEDIR)src \
					-I$(BASEDIR)src/Exception \
					-I$(BASEDIR)src/sql \
					-I$(BASEDIR)src/sql/ast \
					-I$(BASEDIR)src/sql/ast/expr \
					-I$(BASEDIR)src/sql/ast/statement \
					-I$(BASEDIR)src/sql/deps \
					-I$(BASEDIR)src/sql/deps/smhasher \
					-I$(BASEDIR)src/sql/deps/xxhash \
					-I$(BASEDIR)src/sql/dialect \
					-I$(BASEDIR)src/sql/dialect/ads \
					-I$(BASEDIR)src/sql/dialect/ads/visitor \
					-I$(BASEDIR)src/sql/dialect/ads/parser \
					-I$(BASEDIR)src/sql/dialect/mysql \
					-I$(BASEDIR)src/sql/dialect/mysql/ast \
					-I$(BASEDIR)src/sql/dialect/mysql/ast/expr \
					-I$(BASEDIR)src/sql/dialect/mysql/ast/statement \
					-I$(BASEDIR)src/sql/dialect/mysql/ast/clause \
					-I$(BASEDIR)src/sql/dialect/mysql/parser \
					-I$(BASEDIR)src/sql/dialect/mysql/visitor \
					-I$(BASEDIR)src/sql/dialect/mysql/visitor/transform \
					-I$(BASEDIR)src/sql/dialect/mysql/executor \
					-I$(BASEDIR)src/sql/dialect/oracle \
					-I$(BASEDIR)src/sql/dialect/oracle/ast \
					-I$(BASEDIR)src/sql/dialect/oracle/ast/clause \
					-I$(BASEDIR)src/sql/dialect/oracle/ast/expr \
					-I$(BASEDIR)src/sql/dialect/oracle/ast/stmt \
					-I$(BASEDIR)src/sql/dialect/oracle/visitor \
					-I$(BASEDIR)src/sql/dialect/oracle/parser \
					-I$(BASEDIR)src/sql/dialect/antspark \
					-I$(BASEDIR)src/sql/dialect/antspark/ast \
					-I$(BASEDIR)src/sql/dialect/antspark/parser \
					-I$(BASEDIR)src/sql/dialect/antspark/visitor \
					-I$(BASEDIR)src/sql/dialect/blink \
					-I$(BASEDIR)src/sql/dialect/blink/ast \
					-I$(BASEDIR)src/sql/dialect/blink/parser \
					-I$(BASEDIR)src/sql/dialect/blink/vsitor \
					-I$(BASEDIR)src/sql/dialect/clickhouse \
					-I$(BASEDIR)src/sql/dialect/clickhouse/ast \
					-I$(BASEDIR)src/sql/dialect/clickhouse/parser \
					-I$(BASEDIR)src/sql/dialect/clickhouse/visitor \
					-I$(BASEDIR)src/sql/dialect/db2 \
					-I$(BASEDIR)src/sql/dialect/db2/ast \
					-I$(BASEDIR)src/sql/dialect/db2/ast/stmt \
					-I$(BASEDIR)src/sql/dialect/db2/parser \
					-I$(BASEDIR)src/sql/dialect/db2/visitor \
					-I$(BASEDIR)src/sql/dialect/h2 \
					-I$(BASEDIR)src/sql/dialect/h2/parser \
					-I$(BASEDIR)src/sql/dialect/h2/visitor \
					-I$(BASEDIR)src/sql/dialect/hive \
					-I$(BASEDIR)src/sql/dialect/hive/ast \
					-I$(BASEDIR)src/sql/dialect/hive/parser \
					-I$(BASEDIR)src/sql/dialect/hive/stmt \
					-I$(BASEDIR)src/sql/dialect/hive/visitor \
					-I$(BASEDIR)src/sql/dialect/odps \
					-I$(BASEDIR)src/sql/dialect/odps/ast \
					-I$(BASEDIR)src/sql/dialect/odps/parser \
					-I$(BASEDIR)src/sql/dialect/odps/visitor \
					-I$(BASEDIR)src/sql/dialect/oscar \
					-I$(BASEDIR)src/sql/dialect/oscar/ast \
					-I$(BASEDIR)src/sql/dialect/oscar/ast/expr \
					-I$(BASEDIR)src/sql/dialect/oscar/ast/stmt \
					-I$(BASEDIR)src/sql/dialect/oscar/parser \
					-I$(BASEDIR)src/sql/dialect/oscar/visitor \
					-I$(BASEDIR)src/sql/dialect/phoenix \
					-I$(BASEDIR)src/sql/dialect/phoenix/ast \
					-I$(BASEDIR)src/sql/dialect/phoenix/parser \
					-I$(BASEDIR)src/sql/dialect/phoenix/visitor \
					-I$(BASEDIR)src/sql/dialect/postgresql \
					-I$(BASEDIR)src/sql/dialect/postgresql/ast \
					-I$(BASEDIR)src/sql/dialect/postgresql/ast/expr \
					-I$(BASEDIR)src/sql/dialect/postgresql/ast/stmt \
					-I$(BASEDIR)src/sql/dialect/postgresql/parser \
					-I$(BASEDIR)src/sql/dialect/postgresql/visitor \
					-I$(BASEDIR)src/sql/dialect/presto \
					-I$(BASEDIR)src/sql/dialect/presto/ast \
					-I$(BASEDIR)src/sql/dialect/presto/ast/stmt \
					-I$(BASEDIR)src/sql/dialect/presto/parser \
					-I$(BASEDIR)src/sql/dialect/presto/visitor \
					-I$(BASEDIR)src/sql/dialect/sqlserver \
					-I$(BASEDIR)src/sql/dialect/sqlserver/ast \
					-I$(BASEDIR)src/sql/dialect/sqlserver/ast/expr \
					-I$(BASEDIR)src/sql/dialect/sqlserver/ast/stmt \
					-I$(BASEDIR)src/sql/dialect/sqlserver/parser \
					-I$(BASEDIR)src/sql/dialect/sqlserver/visitor \
					-I$(BASEDIR)src/sql/dialect/starrocks \
					-I$(BASEDIR)src/sql/dialect/starrocks/ast \
					-I$(BASEDIR)src/sql/dialect/starrocks/ast/expr \
					-I$(BASEDIR)src/sql/dialect/starrocks/ast/statement \
					-I$(BASEDIR)src/sql/dialect/starrocks/parser \
					-I$(BASEDIR)src/sql/dialect/starrocks/visitor \
					-I$(BASEDIR)src/sql/dialect/transact \
					-I$(BASEDIR)src/sql/dialect/transact/ast \
					-I$(BASEDIR)src/sql/parser \
					-I$(BASEDIR)src/sql/visitor \
					-I$(BASEDIR)src/sql/visitor/functions \
					-I$(BASEDIR)src/sql/builder \
					-I$(BASEDIR)src/sql/builder/impl \
					-I$(BASEDIR)src/sql/repository \
					-I$(BASEDIR)src/sql/repository/function \
					-I$(BASEDIR)src/sql/semantic \
					-I$(BASEDIR)src/sql/transform \
					-I$(BASEDIR)src/sql/transform/function \
					-I$(BASEDIR)src/stat \
					-I$(BASEDIR)src/utils \
					-I$(BASEDIR)src/utils/jdbc \
					-I$(BASEDIR)src/utils/lang \
					-I$(BASEDIR)src/utils/wlogger \

LIBRARY := -L$(RELEASE)/lib/ \

VERSION_INFO="$(shell git log --format='$(TARGET) versioninfo [sha1]:%h [time]:%ci [author]:%cn [commit]:%H [branch]:%D' -1)"
DEFINES += -DVERSION_INFO=\"$(VERSION_INFO)\"
DEFINES += -DSYSTEM_VER=\"$(MAJOR_VERSION).$(MINOR_VERSION).$(PATCH_VERSION)\"
DEFINES += -DUNICODE -DNTRY

THELIB = libparser.so
CXXFLAGS?= -std=c++17 -O2 -DDEBUG -ggdb -fPIC 
#CXXFLAGS?= -Wall -O2 -W 

LDFLAGS += -lpthread 

RTFLAGS := \
	-Wl,-rpath=../lib 

DIR :=  \
			$(BASEDIR)src \
			$(BASEDIR)src/Exception \
			$(BASEDIR)src/sql/parser \
			$(BASEDIR)src/sql/dialect/ads/parser \
			$(BASEDIR)src/sql/dialect/mysql/parser \
			$(BASEDIR)src/sql/dialect/oracle/parser \
			$(BASEDIR)src/sql/dialect/antspark/parser \
			$(BASEDIR)src/sql/dialect/blink/parser \
			$(BASEDIR)src/sql/dialect/clickhouse/parser \
			$(BASEDIR)src/sql/dialect/h2/parser \
			$(BASEDIR)src/sql/dialect/oscar/parser \
			$(BASEDIR)src/sql/dialect/hive/parser \
			$(BASEDIR)src/sql/dialect/odps/parser \
			$(BASEDIR)src/sql/dialect/db2/parser \
			$(BASEDIR)src/sql/dialect/phoenix/parser \
			$(BASEDIR)src/sql/dialect/sqlserver/parser \
			$(BASEDIR)src/sql/dialect/postgresql/parser \
			$(BASEDIR)src/sql/dialect/presto/parser \
			$(BASEDIR)src/sql/dialect/starrocks/parser \
			$(BASEDIR)src/sql/ast \
			$(BASEDIR)src/sql/ast/expr \
			$(BASEDIR)src/sql/ast/statement \
			$(BASEDIR)src/sql/deps \
			$(BASEDIR)src/sql/deps/smhasher \
			$(BASEDIR)src/sql/deps/xxhash \
			$(BASEDIR)src/sql/visitor \
			$(BASEDIR)src/sql/visitor/functions \
			$(BASEDIR)src/sql/builder \
			$(BASEDIR)src/sql/builder/impl \
			$(BASEDIR)src/sql/repository \
			$(BASEDIR)src/sql/repository/function \
			$(BASEDIR)src/sql/semantic \
			$(BASEDIR)src/sql/transform \
			$(BASEDIR)src/sql/transform/function \
			$(BASEDIR)src/stat \
			$(BASEDIR)src/utils \
			$(BASEDIR)src/utils/jdbc \
			$(BASEDIR)src/utils/lang \
			$(BASEDIR)src/utils/logger \
			$(BASEDIR)src/utils/wlogger \
			$(BASEDIR)src/sql/dialect \
			$(BASEDIR)src/sql/dialect/ads \
			$(BASEDIR)src/sql/dialect/ads/visitor \
			$(BASEDIR)src/sql/dialect/mysql \
			$(BASEDIR)src/sql/dialect/mysql/ast \
			$(BASEDIR)src/sql/dialect/mysql/ast/expr \
			$(BASEDIR)src/sql/dialect/mysql/ast/statement \
			$(BASEDIR)src/sql/dialect/mysql/ast/clause \
			$(BASEDIR)src/sql/dialect/mysql/visitor \
			$(BASEDIR)src/sql/dialect/mysql/visitor/transform \
			$(BASEDIR)src/sql/dialect/mysql/executor \
			$(BASEDIR)src/sql/dialect/oracle \
			$(BASEDIR)src/sql/dialect/oracle/ast \
			$(BASEDIR)src/sql/dialect/oracle/ast/clause \
			$(BASEDIR)src/sql/dialect/oracle/ast/expr \
			$(BASEDIR)src/sql/dialect/oracle/ast/stmt \
			$(BASEDIR)src/sql/dialect/oracle/visitor \
			$(BASEDIR)src/sql/dialect/antspark \
			$(BASEDIR)src/sql/dialect/antspark/ast \
			$(BASEDIR)src/sql/dialect/antspark/visitor \
			$(BASEDIR)src/sql/dialect/blink \
			$(BASEDIR)src/sql/dialect/blink/ast \
			$(BASEDIR)src/sql/dialect/blink/vsitor \
			$(BASEDIR)src/sql/dialect/clickhouse \
			$(BASEDIR)src/sql/dialect/clickhouse/ast \
			$(BASEDIR)src/sql/dialect/clickhouse/visitor \
			$(BASEDIR)src/sql/dialect/db2 \
			$(BASEDIR)src/sql/dialect/db2/ast \
			$(BASEDIR)src/sql/dialect/db2/ast/stmt \
			$(BASEDIR)src/sql/dialect/db2/visitor \
			$(BASEDIR)src/sql/dialect/h2 \
			$(BASEDIR)src/sql/dialect/h2/visitor \
			$(BASEDIR)src/sql/dialect/hive \
			$(BASEDIR)src/sql/dialect/hive/ast \
			$(BASEDIR)src/sql/dialect/hive/stmt \
			$(BASEDIR)src/sql/dialect/hive/visitor \
			$(BASEDIR)src/sql/dialect/odps \
			$(BASEDIR)src/sql/dialect/odps/ast \
			$(BASEDIR)src/sql/dialect/odps/visitor \
			$(BASEDIR)src/sql/dialect/oscar \
			$(BASEDIR)src/sql/dialect/oscar/ast \
			$(BASEDIR)src/sql/dialect/oscar/ast/expr \
			$(BASEDIR)src/sql/dialect/oscar/ast/stmt \
			$(BASEDIR)src/sql/dialect/oscar/visitor \
			$(BASEDIR)src/sql/dialect/phoenix \
			$(BASEDIR)src/sql/dialect/phoenix/ast \
			$(BASEDIR)src/sql/dialect/phoenix/visitor \
			$(BASEDIR)src/sql/dialect/postgresql \
			$(BASEDIR)src/sql/dialect/postgresql/ast \
			$(BASEDIR)src/sql/dialect/postgresql/ast/expr \
			$(BASEDIR)src/sql/dialect/postgresql/ast/stmt \
			$(BASEDIR)src/sql/dialect/postgresql/visitor \
			$(BASEDIR)src/sql/dialect/presto \
			$(BASEDIR)src/sql/dialect/presto/ast \
			$(BASEDIR)src/sql/dialect/presto/ast/stmt \
			$(BASEDIR)src/sql/dialect/presto/visitor \
			$(BASEDIR)src/sql/dialect/sqlserver \
			$(BASEDIR)src/sql/dialect/sqlserver/ast \
			$(BASEDIR)src/sql/dialect/sqlserver/ast/expr \
			$(BASEDIR)src/sql/dialect/sqlserver/ast/stmt \
			$(BASEDIR)src/sql/dialect/sqlserver/visitor \
			$(BASEDIR)src/sql/dialect/starrocks \
			$(BASEDIR)src/sql/dialect/starrocks/ast \
			$(BASEDIR)src/sql/dialect/starrocks/ast/expr \
			$(BASEDIR)src/sql/dialect/starrocks/ast/statement \
			$(BASEDIR)src/sql/dialect/starrocks/visitor \
			$(BASEDIR)src/sql/dialect/transact \
			$(BASEDIR)src/sql/dialect/transact/ast \
			$(BASEDIR)src/sql \
			$(BASEDIR)src/wall \
			$(BASEDIR)src/wall/spi \
			$(BASEDIR)src/wall/violation \


SRC_CXX := $(foreach d, $(DIR), $(wildcard $(d)/*.cc))
SRC_CPP := $(foreach d, $(DIR), $(wildcard $(d)/*.cpp))
OBJ_CXX := $(patsubst %.cc, %.o, $(SRC_CXX))
OBJ_CPP := $(patsubst %.cpp, %.o, $(SRC_CPP))
DEP_CXX := $(patsubst %.o, %.d, $(OBJ_CXX))
DEP_CPP := $(patsubst %.o, %.d, $(OBJ_CPP))

OBJ_CPP := $(subst ../src/sql/parser/Token.o,,$(OBJ_CPP))
OBJ_CPP := ../src/sql/parser/Token.o $(OBJ_CPP)

OBJ_CPP := $(subst ../src/sql/ast/statement/SQLObjectType.o,,$(OBJ_CPP))
OBJ_CPP := ../src/sql/ast/statement/SQLObjectType.o $(OBJ_CPP)


AM_DEFAULT_VERBOSITY ?= 0

AM_V_GEN = $(am__v_GEN_$(V))
am__v_GEN_ = $(am__v_GEN_$(AM_DEFAULT_VERBOSITY))
am__v_GEN_0 = @echo "  GEN     " $@;
am__v_GEN_1 =
AM_V_at = $(am__v_at_$(V))
am__v_at_ = $(am__v_at_$(AM_DEFAULT_VERBOSITY))
am__v_at_0 = @
am__v_at_1 =

AM_V_CC = $(am__v_CC_$(V))
am__v_CC_ = $(am__v_CC_$(AM_DEFAULT_VERBOSITY))
am__v_CC_0 = @echo "  CC      " $@;
am__v_CC_1 =

AM_V_CCLD = $(am__v_CCLD_$(V))
am__v_CCLD_ = $(am__v_CCLD_$(AM_DEFAULT_VERBOSITY))
ifneq ($(SKIP_LINK), 1)
am__v_CCLD_0 = @echo "  CCLD    " $@;
am__v_CCLD_1 =
else
am__v_CCLD_0 = @echo "  !CCLD   " $@; true skip
am__v_CCLD_1 = true skip
endif
AM_V_AR = $(am__v_AR_$(V))
am__v_AR_ = $(am__v_AR_$(AM_DEFAULT_VERBOSITY))
am__v_AR_0 = @echo "  AR      " $@;
am__v_AR_1 =


all: $(RELEASE)/lib/$(THELIB) $(RELEASE)/bin/test_CharTypes $(RELEASE)/bin/test_Keywords $(RELEASE)/bin/test_Lexer $(RELEASE)/bin/test_Token $(RELEASE)/bin/test_SQLParser $(RELEASE)/bin/test_SQLExprParser $(RELEASE)/bin/bench

$(RELEASE)/lib/$(THELIB): $(OBJ_CXX) $(OBJ_CPP) 
	g++ $(CXXFLAGS) -shared $(OBJ_CXX) $(OBJ_CPP) $(LIBRARY) $(RTFLAGS) $(LDFLAGS) -Wl,-soname,$(THELIB) -o $(RELEASE)/lib/$(THELIB).$(MAJOR_VERSION).$(MINOR_VERSION).$(PATCH_VERSION)
	ln -fs ./$(THELIB).$(MAJOR_VERSION).$(MINOR_VERSION).$(PATCH_VERSION) $(RELEASE)/lib/$(THELIB).$(MAJOR_VERSION)
	ln -fs ./$(THELIB).$(MAJOR_VERSION) $(RELEASE)/lib/$(THELIB)


$(RELEASE)/bin/test_CharTypes: test/CharTypes_test.o 
	g++ $(CXXFLAGS) $(LIBRARY) -L./ $^ $(RTFLAGS) $(LDFLAGS) -lparser -o $@

$(RELEASE)/bin/test_Keywords: test/Keywords_test.o 
	g++ $(CXXFLAGS) $(LIBRARY) -L./ $^ $(RTFLAGS) $(LDFLAGS) -lparser -o $@

$(RELEASE)/bin/test_Token: test/Token_test.o 
	g++ $(CXXFLAGS) $(LIBRARY) -L./ $^ $(RTFLAGS) $(LDFLAGS) -lparser -o $@

$(RELEASE)/bin/test_Lexer: test/Lexer_test.o 
	g++ $(CXXFLAGS) $(LIBRARY) -L./ $^ $(RTFLAGS) $(LDFLAGS) -lparser -o $@

$(RELEASE)/bin/test_SQLParser: test/SQLParser_test.o 
	g++ $(CXXFLAGS) $(LIBRARY) -L./ $^ $(RTFLAGS) $(LDFLAGS) -lparser -o $@

$(RELEASE)/bin/test_SQLExprParser: test/SQLExprParser_test.o 
	g++ $(CXXFLAGS) $(LIBRARY) -L./ $^ $(RTFLAGS) $(LDFLAGS) -lparser -o $@

$(RELEASE)/bin/bench: test/bench.o 
	g++ $(CXXFLAGS) $(LIBRARY) -L./ $^ $(RTFLAGS) $(LDFLAGS) -lparser -o $@

-include $(DEP_CXX) $(DEP_CPP)

%.o : %.cc
	$(AM_V_CC)$(CXX) -c $(CXXFLAGS) $(DEFINES) $(INCLUDE) $< -o $@

%.o : %.cpp
	$(AM_V_CC)$(CXX) -c $(CXXFLAGS) $(DEFINES) $(INCLUDE) $< -o $@

%.d : %.cc
	@$(CXX) -MM $< $(CXXFLAGS) | sed 's/$(notdir $*)\.o/$(subst /,\/,$*).o $(subst /,\/,$*).d/g' > $@

%.d : %.cpp
	@$(CXX) -MM $< $(CXXFLAGS) | sed 's/$(notdir $*)\.o/$(subst /,\/,$*).o $(subst /,\/,$*).d/g' > $@

clean:
	rm -f $(OBJ_CXX) $(OBJ_CPP) $(DEP_CXX) $(DEP_CPP) *.a *.so* *.o $(RELEASE)/lib/$(THELIB) $(RELEASE)/bin/test_CharTypes $(RELEASE)/bin/test_Keywords $(RELEASE)/bin/test_Lexer $(RELEASE)/bin/test_Token $(RELEASE)/bin/test_SQLParser $(RELEASE)/bin/test_SQLExprParser $(RELEASE)/bin/bench

cleanObj:
	rm -rf *.o 

############### from parser directory ################################


